//
//  Tool.m
//  iOSProjectExtension
//
//  Created by Apple on 2020/1/9.
//  Copyright © 2020 lidianchao. All rights reserved.
//

#import "Tool.h"

@implementation Tool
/**
 储存到plist里面
 */
+ (void)savePlistFileWithObject:(id )arrayF withName:(NSString *)name {
    
    if (!arrayF) {
        return;
    }
    
    NSArray*array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
    NSString*cachePath = array[0];
    NSString*filePathName = [cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", name]];

    id saveObject = arrayF;
    if([arrayF isKindOfClass:[NSString class]])
    {
        saveObject = @[arrayF];
    }

    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:saveObject options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    [jsonString writeToFile:filePathName atomically:YES encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@", filePathName);
}
+ (id )getPlistFileWithName:(NSString *)name withPlistType:(plist_type)type {
    id obeject = [[NSObject alloc] init];
    NSArray*array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
    NSString*cachePath = array[0];
    NSString*filePathName = [cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", name]];

    NSString *aesStre = [NSString stringWithContentsOfFile:filePathName encoding:NSUTF8StringEncoding error:nil];
    switch (type) {
        case plist_Arrray: {
            obeject = [Tool stringToJSON:aesStre];
        }
            break;
        case plist_String: {
            NSArray *arr = [Tool stringToJSON:aesStre];
            obeject = arr[0];
        }
            break;
        case plist_Dictionary: {
            obeject = [Tool dictionaryWithJsonString:aesStre];
        }
            break;
        default:
            break;
    }
    return obeject;
}
+ (void)clearPlist:(NSString *)name
{
    [self savePlistFileWithObject:@"" withName:name];
}

//存储string
+ (void)saveStringToUserDefaults:(NSString *)string key:(NSString *)key
{
    NSUserDefaults *stand = [NSUserDefaults standardUserDefaults];
    [stand setObject:string forKey:key];
    [stand synchronize];
}
+ (NSString *)getStringValueFromUserDefaults:(NSString *)key
{
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    return string;
}
//存储array
+ (void)saveArrayToUserDefaults:(NSArray *)array key:(NSString *)key
{
    NSUserDefaults *stand = [NSUserDefaults standardUserDefaults];
    NSString *jsonString = [Tool jsonStringWithObject:array];
    [stand setObject:jsonString forKey:key];
    [stand synchronize];
}
+ (NSArray *)getArrayValueFromUserDefaults:(NSString *)key
{
    NSString *jsonString = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    NSArray *array = [Tool arrayToJSON:jsonString];
    return array;
}
+ (void)saveDicToUserDefaults:(NSDictionary *)dic key:(NSString *)key
{
    NSUserDefaults *stand = [NSUserDefaults standardUserDefaults];
    NSString *jsonString = [Tool jsonStringWithObject:dic];
    [stand setObject:jsonString forKey:key];
    [stand synchronize];
}
+ (NSDictionary *)getDicFromDefaults:(NSString *)key
{
    NSString *jsonString = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    NSDictionary *dic = [Tool dictionaryWithJsonString:jsonString];
    return dic;
}
#pragma -mark private

+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                         
                                                        options:NSJSONReadingMutableContainers
                         
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}
+ (NSArray *)stringToJSON:(NSString *)jsonStr {
    if (jsonStr)
    {
        id tmp = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
        
        if (tmp)
        {
            if ([tmp isKindOfClass:[NSArray class]])
            {
                return tmp;
            }
            else if([tmp isKindOfClass:[NSString class]]
                      || [tmp isKindOfClass:[NSDictionary class]])
            {
                return [NSArray arrayWithObject:tmp];
            }
            else
            {
                return nil;
            }
        }
        else
        {
            return nil;
        }
    }
    else
    {
        return nil;
    }
}
/************************************对象转Json穿************************************************************/
+ (NSString *)jsonStringWithObject:(id)object
{
    id saveObject = object;
    if([saveObject isKindOfClass:[NSString class]])
    {
        saveObject = @[object];
    }
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:saveObject options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}
+ (NSArray *)arrayToJSON:(NSString *)jsonStr {
    if (jsonStr)
    {
        id tmp = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
        
        if (tmp)
        {
            if ([tmp isKindOfClass:[NSArray class]])
            {
                return tmp;
            }
            else if([tmp isKindOfClass:[NSString class]]
                    || [tmp isKindOfClass:[NSDictionary class]])
            {
                return [NSArray arrayWithObject:tmp];
            }
            else
            {
                return nil;
            }
        }
        else
        {
            return nil;
        }
    }
    else
    {
        return nil;
    }
}
@end
